package FM2_15577.front_end;

import java.math.BigDecimal;
import org.json.JSONObject;

public class Account {
    private long aid = 0; // 存储当前登录账户ID，0表示未登录
    private static Client client = new Client(); // 负责完成与服务器间通信
    
    public Account(long _aid) {
        this.aid = _aid;
    }

    public long getAid() {
        return this.aid;
    }

    public BigDecimal getBalance() throws Exception {
        BigDecimal ret = null;
        JSONObject res = null; // 存储请求结果
        if (this.getAid() == 0L) {
            ret = BigDecimal.ONE.negate();
            // System.err.println("Please log in first!");
            throw new Exception("No account login!");
        }
        else {
            JSONObject object = new JSONObject(); // 请求内容object
            object.put("type", "getBalance"); // 请求类型
            // 请求附带信息
            object.put("aid", this.getAid());
            try {
                res = client.send(object);
            } catch (Exception e) {
                // System.err.println(e.getMessage());
                throw e;
            }
            if (res == null) {
                ret = BigDecimal.ONE.negate();
                throw new Exception("No response from server!");
            }
            else{
                if (res.getString("result").contentEquals("success")) {
                    // System.err.println("Get balance successfully");
                    ret = new BigDecimal(res.getString("balance"));
                }
                else {
                    ret = BigDecimal.ONE.negate();
                    // System.err.println("Get balance failed: "+res.getString("reason"));
                    throw new Exception(res.getString("reason"));
                }
            }
        }
        return ret;
    }

    public BigDecimal deposit(String amount) throws Exception { // 返回存款后余额，amount为正实数且不超过两位小数
        BigDecimal ret;
        JSONObject res = null; // 存储请求结果
        if (this.getAid() == 0L) {
            ret = BigDecimal.ONE.negate();
            // System.err.println("Please log in first!");
            throw new Exception("No account login!");
        }
        else {
            JSONObject object = new JSONObject(); // 请求内容object
            object.put("type", "deposit"); // 请求类型
            // 请求附带信息
            object.put("aid", this.getAid());
            object.put("amount", amount);
            try {
                res = client.send(object);
            } catch (Exception e) {
                // System.err.println(e.getMessage());
                throw e;
            }
            if (res == null) {
                ret = BigDecimal.ONE.negate();
                throw new Exception("No response from server!");
            }
            else{
                if (res.getString("result").contentEquals("success")) {
                    // System.err.println("Deposit successfully");
                    ret = new BigDecimal(res.getString("balance"));
                }
                else {
                    ret = BigDecimal.ONE.negate();
                    // System.err.println("Deposit failed: "+res.getString("reason"));
                    throw new Exception(res.getString("reason"));
                }
            }
        }
        return ret;
    }
    
    public BigDecimal withdraw(String amount) throws Exception { // 返回取款后余额，amount为正实数且不超过两位小数
        BigDecimal ret = null;
        JSONObject res = null; // 存储请求结果
        if (this.getAid() == 0L) {
            ret = BigDecimal.ONE.negate();
            // System.err.println("Please log in first!");
            throw new Exception("No account login!");
        }
        else {
            JSONObject object = new JSONObject(); // 请求内容object
            object.put("type", "withdraw"); // 请求类型
            // 请求附带信息
            object.put("aid", this.getAid());
            object.put("amount", amount);
            try {
                res = client.send(object);
            } catch (Exception e) {
                // System.err.println(e.getMessage());
                throw e;
            }
            if (res == null) {
                ret = BigDecimal.ONE.negate();
                throw new Exception("No response from server!");
            }
            else{
                if (res.getString("result").contentEquals("success")) {
                    // System.err.println("Withdraw successfully");
                    ret = new BigDecimal(res.getString("balance"));
                }
                else {
                    ret = BigDecimal.ONE.negate();
                    // System.err.println("Withdraw failed: "+res.getString("reason"));
                    throw new Exception(res.getString("reason"));
                }
            }
        }
        return ret;
    }

    public BigDecimal transfer(long target, String amount) throws Exception { // 返回转账后余额，amount为正实数且不超过两位小数
        BigDecimal ret = null;
        JSONObject res = null; // 存储请求结果
        if (this.getAid() == 0L) {
            ret = BigDecimal.ONE.negate();
            // System.err.println("Please log in first!");
            throw new Exception("No account login!");
        }
        else {
            JSONObject object = new JSONObject(); // 请求内容object
            object.put("type", "transfer"); // 请求类型
            // 请求附带信息
            object.put("aid", this.getAid());
            object.put("taid", target);
            object.put("amount", amount);
            try {
                res = client.send(object);
            } catch (Exception e) {
                // System.err.println(e.getMessage());
                throw e;
            }
            if (res == null) {
                ret = BigDecimal.ONE.negate();
                throw new Exception("No response from server!");
            }
            else{
                if (res.getString("result").contentEquals("success")) {
                    // System.err.println(String.format("Transfer successfully: target=%d, amount=%s", target, amount));
                    ret = new BigDecimal(res.getString("balance"));
                }
                else {
                    ret = BigDecimal.ONE.negate();
                    // System.err.println("Transfer failed: "+res.getString("reason"));
                    throw new Exception(res.getString("reason"));
                }
            }
        }
        return ret;
    }

    public void updateInfo(String name, String cipher, long phone, char sex) throws Exception {
        JSONObject res = null; // 存储请求结果
        if (this.getAid() == 0L) {
            // System.err.println("Please log in first!");
            throw new Exception("No account login!");
        }
        else {
            JSONObject object = new JSONObject(); // 请求内容object
            object.put("type", "updateInfo"); // 请求类型
            // 请求附带信息
            object.put("aid", this.getAid());
            object.put("name", name);
            object.put("cipher", cipher);
            object.put("phone", phone);
            object.put("sex", sex);
            try {
                res = client.send(object);
            } catch (Exception e) {
                // System.err.println(e.getMessage());
                throw e;
            }
            if (res == null) {
                throw new Exception("No response from server!");
            }
            else{
                if (res.getString("result").contentEquals("success")) {
                    // System.err.println(String.format("Update information successfully: name=%s, cipher=%s, phone=%d, sex=%c", name, cipher, phone, sex));
                }
                else {
                    // System.err.println("Update information failed: "+res.getString("reason"));
                    throw new Exception(res.getString("reason"));
                }
            }
        }
    }
}
